﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DAL
{
    public class ThietBiProvider
    {
        public static List<ThietBi> LayDanhSachThietBi()
        {
            List<ThietBi> lst = new List<ThietBi>();
            using(QLTTTHEntities context = new QLTTTHEntities())
            {
                var query = (from u in context.ThietBis select u);
                foreach(var row in query)
                {
                    ThietBi tb = new ThietBi();
                    tb.MaTB = row.MaTB;
                    tb.TenTB = row.TenTB;
                    tb.ChucNang = row.ChucNang;
                    tb.TrangThai = row.TrangThai;
                    tb.MaPhong = row.MaPhong;

                    lst.Add(tb);
                }
            }
            return lst;
        }
        public static bool XoaThietBi(int maTB)
        {
            try
            {
                using (var context = new QLTTTHEntities())
                {
                    var query = (from u in context.ThietBis
                                 where u.MaTB.Equals(maTB)
                                 select u).First();

                    context.ThietBis.Remove(query);
                    context.SaveChanges();
                }
                return true;
            }
            catch
            {
                return false;
            }
        }
        public static bool CapNhatThietBi(ThietBi tb)
        {
            try
            {
                using (var context = new QLTTTHEntities())
                {
                    var query = (from u in context.ThietBis
                                 where u.MaTB.Equals(tb.MaTB)
                                 select u).First();
                    query.TenTB = tb.TenTB;
                    query.ChucNang = tb.ChucNang;
                    query.TrangThai = tb.TrangThai;
                    query.MaPhong = tb.MaPhong;

                    context.SaveChanges();
                }
                return true;
            }
            catch
            {
                return false;
            }
        }
        public static int ThemThietBi(ThietBi tb)
        {
            int maTB = -1;
            try
            {
                using (var context = new QLTTTHEntities())
                {
                    context.ThietBis.Add(tb);
                    context.SaveChanges();
                    maTB = tb.MaTB;
                }
            }
            catch
            {

            }
            return maTB;
        }
    }
}
